#include <Arduino.h>
#include "Wire.h"
#include "29005.h"
#include "U8x8libcode.h"
#include <arduino-timer.h>
#include "U8glibcode.h"
#include "U8g2code.h"
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
/*****************************功能定义********************************/
#define PS_DYN_K            1
#define PS_DYN_K_TIMER      0
#define PS_GREASE           1
#define DEBUG_LOG           1
#define BLINKER             1
#define ARDUINO_TIMER            1
#define BLINKER_TIMER            0
bool mn_sensor_ps_polling_mode = false;
bool mn_sensor_als_polling_mode = true;
/*****************************U8glib********************************/
int a0 = 1; //PS enable状态
int b0 = 1; //ALS enable状态
int c0 = 1; //als int状态
int d0 = 0; //ps int状态
int a1 = 0; //打开als
int b1 = 0; //打开ps
int c1 = 0; //更新als 中断
int d1 = 0; //更新ps中断
int a3 = 0; //oled显示als
int b3 = 1; //oled显示ps
int c3 = 0; //oled显示ps thd
int a4 = 0; //oled亮度调节
int a5 = 0; //reg打印

/*****************************调试选项********************************/

auto timer = timer_create_default(); // create a timer with default settings
/******************************************************************************
    ALS Light source
 ******************************************************************************/
typedef enum
{
  CMC_BIT_RAW        = 0x0,
  CMC_BIT_PRE_COUNT       = 0x1,
} CMC_ALS_REPORT_TYPE;
unsigned long waittime0 = 30000;
unsigned long waittime = 30000;

int liangdu = 200;
int maxliangdu = 200;
int minliangdu = 200;
int dislaystatus = 1;
uint32_t lsrc_lux = 0;
uint32_t lsrc_ratio = 0;
uint8_t  als_staust;
int  SensorINPUT = D7;
uint16_t pilt, piht;
/***************变量定义**********************/
uint16_t ch0, ch1, ch2;
uint16_t als_ag_h2m_thd;
uint16_t als_ag_m2h_thd;
uint16_t als_ag_m2l_thd;
uint16_t als_ag_l2m_thd;
uint16_t als_ag_l2ll_thd;
uint16_t als_ag_ll2l_thd;
uint16_t i2cData[14];
uint16_t mTask_txBuf[14];
static int mn_sensor_I2C_Write(uint8_t reg, uint8_t value);
static int mn_sensor_I2C_Write_Block(uint8_t reg, uint8_t value[], uint8_t len);
uint16_t mn_sensor_I2C_Read(uint16_t registerAddress, uint16_t *data, uint8_t nbytes);
uint16_t i2cWrite(uint8_t registerAddress, uint8_t *data, uint8_t length, bool sendStop);
uint16_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop);
void initial_global_variable(void);
static int32_t mn78xxx_read_ps();
static void mn_sensor_update_mode(void);
static int mn_sensor_int_conf();
static int mn_sensor_int_conf();
static int write_pxl_sel();
static int write_als_conf();
static int write_ps_conf();
static int write_ag_conf();
uint16_t set_psensor_intr_threshold(uint16_t ailt, uint16_t aiht);
int set_lsensor_intr_threshold(uint16_t ailt, uint16_t aiht);
static int select_pixel_scale();
int set_ag_threshold(uint16_t reg_addr, uint16_t ag_thd);
static uint16_t mn78_sensor_als_ag_calc_new_thd(uint16_t intt, uint16_t enh, uint16_t gain);
//void mn78xxx_setup_eint();
void print_message();
static int mn_sensor_read_ps_thd();
bool toggle_polling_reading(void *);
bool print_message(void *);
ICACHE_RAM_ATTR void mn78xxx_setup_eint();
/*********************其他变量**********************
  uint16_t serial_putc( char c, struct __file * )
  {
  Serial.write( c );
  return c;
  }
  void printf_begin(void)
  {
  fdevopen( &serial_putc, 0 );
  }

*********************初始化**********************/
void setup()
{
  //pinMode(3, INPUT);
  //pinMode(SensorINPUT, INPUT_PULLUP);
  Serial.begin(115200);  //蓝牙串口
  Wire.begin();  //I2C总线
  //  WIFI_Set();
#ifdef U8g2
  initu8g2code();
#endif
  /*********************初始化**********************/
#ifdef U8x8lib
  initu8x8code();
#endif
  /*********************初始化**********************/
#ifdef U8glib
  initu8glibcode();
#endif
  /*********************BLINKER**********************/
#if BLINKER
  initblinker();
#endif
  /*********************BLINKER**********************/
  // printf_begin();
  initTJCK028();//串口屏显示
  initmn78911();
}
void enable_alsps() {
  if (a1 == 1) {
    a1 = 0;
    if (a0 == 1) {
      mn78xxx_ps_enable();
    }
    else if (a0 == 0) {
      mn78xxx_ps_disable();
    }
    Serial.printf("[%s]: xxxxxxxxxxxxxxxxx als/ps(%d/%d) \n", __func__, mn78xxx_dev_als_enable, mn78xxx_dev_ps_enable);
  }
  if (b1 == 1) {
    b1 = 0;
    if (b0 == 1) {
      mn78xxx_als_enable();
    }
    else if (b0 == 0) {
      mn78xxx_als_disable();
    }
    Serial.printf("[%s]: xxxxxxxxxxxxxxxxx als/ps(%d/%d) \n", __func__, mn78xxx_dev_als_enable, mn78xxx_dev_ps_enable);
  }

  if (c1 == 1) {

    c1 = 0;
    if (c0 == 1) {
      mn_sensor_ps_polling_mode = false;
    }
    else if (c0 == 0) {
      mn_sensor_ps_polling_mode = true;
    }
    Serial.printf("[%s]: =====> als_polling_mode=%d, ps_polling_mode:%d\n", __func__, mn_sensor_als_polling_mode, mn_sensor_ps_polling_mode);
    initial_global_variable();
  }
  if (d1 == 1) {

    d1 = 0;
    if (d0 == 1) {
      mn_sensor_als_polling_mode  = false;
    }
    else if (d0 == 0) {
      mn_sensor_als_polling_mode  = true;
    }
    Serial.printf("[%s]: =====> als_polling_mode=%d, ps_polling_mode:%d\n", __func__, mn_sensor_als_polling_mode, mn_sensor_ps_polling_mode);
    initial_global_variable();
    //Blinker.run();
  }
}
/*********************************************************************************************************
  OLED第零屏显示，als

*********************************************************************************************************/
static int OLED_0() {
  drawpsrawdata(mn_sensor_ps_data_ir_data , mn_sensor_ps_data_pdata, mTask_data_prox_data);
  return 0;
}
/*********************************************************************************************************
  OLED第一屏显示,ps

*********************************************************************************************************/
static int OLED_1() {
  drawalsrawdata(mn_sensor_als_data_channel0, mn_sensor_als_data_channel1, mn_sensor_als_ag_current_stage, mn_sensor_als_data_lux);
  return 0;
}
/*********************************************************************************************************
  OLED第二屏显示，WiFi，温度

*********************************************************************************************************/
static int OLED_2() {
  mn_sensor_read_ps_thd();
  drawpsthd(pilt, piht, mn_sensor_ps_dynk_min_ps_raw_data);
  return 0;
}
/*********************************************************************************************************
  OLED第三屏显示，天气

*********************************************************************************************************/
void OLED_3() {
  oledliangdu(mn_sensor_als_data_lux);
}
/*********************************************************************************************************
  OLED第四屏显示，空气

*********************************************************************************************************/
static int oled_display()
{
  if (alsps_oled_flag == true)
  {
    alsps_oled_flag = false;
    if (a4 == 1)
    {
      OLED_3();
    }
    if (a3 == 1)
    {
      OLED_0();
    }
    else if (b3 == 1)
    {
      OLED_1();
    }
    else if (c3 == 1)
    {
      OLED_2();
    }
#if BLINKER
    blinker();
#endif
  }
  return 0;
}
void oledliangdu(int32_t value)
{
  liangdu = map(value, 0, 10000, 0, 255);

  if (liangdu > 1)
  {
    u8g2.setContrast(liangdu);
    dislaystatus = 0;
  }
  else
  {
    dislaystatus = 1;
  }
  u8g2.setPowerSave(dislaystatus);
}
void loop()
{
  oled_display();
  enable_alsps();
#if BLINKER
  blinker();
#endif
  //********************************中断服务程序********************************** //
  if ( (mn_sensor_als_polling_mode == false) | (mn_sensor_ps_polling_mode == false) )
  {
    mn78xxx_eint_handler();
  }
  //********************************轮询服务程序********************************** //
  if ( (mn_sensor_als_polling_mode == true) | (mn_sensor_ps_polling_mode == true) )
  {
    mn78xxx_als_sample();
    mn78xxx_ps_sample();
  }
  //********************************timer服务程序********************************************/
#if ARDUINO_TIMER
  timer.tick(); // tick the timer
#endif
  if (alsps_TJC_flag == true)
  {
    alsps_TJC_flag = false;
    TJCK028();
  }
  /********************************timer服务程序********************************************
    unsigned long currentMillis = millis(); // store the current time
    #ifdef U8g2//emi by 20210302
    if (currentMillis <= waittime0) {
    if (a3 == 1) {
      drawalsrawdata(mn_sensor_als_data_channel0, mn_sensor_als_data_channel1, mn_sensor_als_ag_current_stage);//oled显示CH0 CH1 CH2
    }
    }
    else if (currentMillis >= waittime0) {
    if (b3 == 1) {
      drawpsrawdata(mn_sensor_ps_data_ir_data, mn_sensor_ps_data_pdata, mTask_data_prox_data);//oled显示CH0 CH1 CH2
    }
    }
    if (currentMillis >= waittime0 + waittime)
    waittime0 = currentMillis + waittime;
    #else
    //drawalsrawdata(mn_sensor_als_data_channel0, mn_sensor_als_data_channel1, mn_sensor_als_ag_current_stage);//oled显示CH0 CH1 CH2
    #endif
    #ifdef U8x8lib
    #ifdef U8x8liblog
    if (currentMillis <= waittime0)
    U8x8lib_drawr_als_wdata(mn_sensor_als_data_channel0, mn_sensor_als_data_channel1, mn_sensor_als_ag_current_stage);//oled显示CH0 CH1 CH2mn_sensor_als_ag_current_stage
    else if (currentMillis >= waittime0)
    U8x8lib_drawr_ps_wdata(mn_sensor_ps_data_ir_data, mn_sensor_ps_data_pdata, mTask_data_prox_data);//oled显示CH0 CH1 CH2
    if (currentMillis >= waittime0 + waittime)
    {
    waittime0 = currentMillis + waittime;
    }
    #else
    //drawalsrawdata(mn_sensor_als_data_channel0, mn_sensor_als_data_channel1, mn_sensor_als_ag_current_stage);//oled显示CH0 CH1 CH2
    #endif
    #endif
  ******/
  //reg();
  //delay(200);
}
